package model.protocal
{
	import com.adobe.utils.StringUtil;
	import com.kingnet.utils.StringUtil;
	
	import flash.globalization.StringTools;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayList;
	import mx.logging.ILogger;
	import mx.utils.StringUtil;
	
	import model.protocal.base.IProtocol;
	
	import utils.BytesUtil;
	import utils.LogUtil;
	import utils.StringUtils;
	
	/***
	 *  station_info为10577906，
	 * 那么 MNC=00  
	 * MCC=460
	 * MNC,MCC固定字段
	 *   
	 * LAC=1057  
	 * CID=7906
	 * 
	 * @reference
	 * 基站一共四个参数，cid lac mnc mcc, 
	 * 把这四个参数的值分别转成16进制（前面补0凑个8个字符）
	 * 然后加上前后的固定的那串，拼接起来，再转成2进制流
	 * 
	 *  @Time: 2012 10:28:26 PM 
	 **/ 
	public class GoogleGLMParseProtocol implements IProtocol
	{
		public static var NAME : String = "GoogleGLMParseProtocol.as";
		private var logger:ILogger =  LogUtil.getInst().getLogger( NAME );
		public static var MAX_LEN_PARAM : int = 8;
		//协议前缀
		private var PREFIX_STR : String = "000E00000000000000000000000000001B0000000000000000000000030000";
		public var MNC : String = "00"; 
		public var MCC : String = "460";
		public var LAC : String = "";
		public var CID : String = "";
		
		protected function getBaseProtocalRootUrl():String
		{
			return "http://www.google.com/glm/mmap";
		}
		
		public function getPostUrl():String{
			return getBaseProtocalRootUrl();
		}
		
		/**
		 * 格式化参数
		 *  补全参数为8位，不够前置0
		 */
		private function formatParam(str:String):String {
			var outStr:String = "";
			if ( str!=null && str.length > 0 && str.length < MAX_LEN_PARAM ) {
				outStr = createStrBylength(MAX_LEN_PARAM).concat(str);
			} else {
				throw new Error("字符串长度超过8位!");			
			}
			return outStr;
		}
		
		/**
		 * 根据长度创建字符串
		 */
		private function createStrBylength(len:int, replaceStr:String="0"):String
		{
			var out:String = "";
			for (var ii:int=0;ii<len;ii++){
				out += replaceStr;
			}
			return out;
		}
		
		/**
		 * 组合协议
		 * cid lac mnc mcc, 
		 */
		public function getProtocalString():String
		{
			return PREFIX_STR 
					  + formatParam(StringUtils.toHex(this.CID))
					  + formatParam(StringUtils.toHex(this.LAC))
					  + formatParam(StringUtils.toHex(this.MNC))
					  + formatParam(StringUtils.toHex(this.MCC));
		}
		
		/**
		 * 回调函数
		 */
		public function onResponseHandler(evt:Object):void
		{
			var list:ByteArray = ByteArray(evt.currentTarget.data);
			logger.info("返回:" + BytesUtil.readString(list));
		}
	}
}